package cn.tedu.DButil;
import cn.tedu.basic.SaveAP;
import cn.tedu.basic.Student;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.Scanner;
public class DBStudent {
    private static final Scanner SCANNER = new Scanner(System.in);

    /*通过序列化将学生信息保存到本地*/
    public static void saveStudent(Student obj, String file) {
        DButil.method(file, obj);
    }
    /*使用反序列化将学生从本地读入*/

    public static void inputStudent(String name, Class cla) {
        System.out.println(DButil.method2("G:\\Project01\\Student\\" + name + ".txt", cla));
    }
    /*学生修改基本信息*/
    public static void changStudent(String name, Class cla) {

        System.out.println("请输入学号");
        String id = SCANNER.next();
        System.out.println("请输入年龄");
        int age = SCANNER.nextInt();
        System.out.println("请输入班级");
        String clazz = SCANNER.next();
        System.out.println("请输入账号");
        String zh = SCANNER.next();
        System.out.println("请输入密码");
        String password = SCANNER.next();
        Student student = new Student(id, name, age, clazz, zh, password);
        DButil.method("G:\\Project01\\Student\\" + name + ".txt", student);
        DBStudent.saveStudent(student, "G:\\Project01\\Student\\" + name + ".txt");
        System.out.println("完成修改，修改后您的信息为" + student);
        SaveAP saveAP = new SaveAP(zh, password);
        savepassword(saveAP, name);
        System.out.println("账号密码修改成功");
    }
    /*试卷本地读取*/

    public static void duqu(String name) {
        Reader reader = null;
        try {
            reader = new BufferedReader(new FileReader("G:\\Project01\\试题\\试题.txt"));
            char b[] = new char[8000];
            int a = 0;
            while ((a = reader.read(b, 0, b.length)) != -1) {
                if (a == b.length) {

                    System.out.print(Arrays.toString(b));
                } else {
                    for (int i = 0; i < a; i++) {
                        System.out.print(b[i]);
                    }
                }
            }
        } catch (IOException e) {
            System.out.println("试题丢失");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*获取成绩*/
    @Test
    public static void getscore(String name) {

        BufferedReader bufferedReader = null;//字符输入流用来读取答案


        System.out.println();
        System.out.println("不会的请用空格代替,考试时间为40s，做完*键结束");
        System.out.println();
        System.out.println("请输入答案");
        final String next =SCANNER.next();
        String s = next.toUpperCase();


        char[] chars = s.toCharArray();
        int score = 0;//记录成绩
        try {
            bufferedReader = new BufferedReader(new FileReader("G:\\Project01\\答案\\答案.txt"));
            int b = 0;
            char a[] = new char[1000];

            while ((b = bufferedReader.read(a, 0, a.length)) != -1) {
                for (int i = 0; i < b; i++) {
                    if ((char)a[i] == chars[i]) {
                        score += 5;
                    }
                }
            }
            System.out.println("做题结束您的成绩为\t"+score);
        } catch (Exception e) {
            System.out.println("答案丢失");
        }
        Student student = DButil.method2("G:\\Project01\\Student\\" + name + ".txt", Student.class);
        student.setScore(score);
        DButil.method("G:\\Project01\\Student\\" + name + ".txt", student);
    }

    /*本方法用来保存账号密码*/
    public static void savepassword(SaveAP saveAP, String name) {
        DButil.method("G:\\Project01\\password\\" + name + ".txt", saveAP);


    }

    /*登陆验证*/
    public static boolean yanzheng(String name, SaveAP saveAP) {
        SaveAP saveAP1 = null;
        try {
            saveAP1 = DButil.method1("G:\\Project01\\password\\" + name + ".txt", saveAP);
        } catch (Exception e) {
            System.out.println("密码错误");
        }
        if (saveAP1.equals(saveAP)) {
            return true;
        } else {
            return false;
        }
    }
}
